package com.example.demo.service;

import com.example.demo.dao.DeleteDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@Service
public class DeleteServiceImpl implements DeleteService{
    @Autowired
    private DeleteDao dd;
    public boolean simpleDelete(Map<String,String[]> params){
        try{
            Class c=dd.getClass();
            Map<String,Object> sqlParams=new HashMap<>();
            String interfaceName="";
            for(String key:params.keySet()){
                if(key.equalsIgnoreCase("flag")){
                    //提取flag获取接口名
                    interfaceName=params.get("flag")[0];
                }
                else{
                    //获取sql参数
                    sqlParams.put(key,params.get(key)[0]);
                }
            }
            Method[] methods=c.getMethods();
            for (int i = 0; i < methods.length; i++) {
                if(interfaceName.equals(methods[i].getName())){
                    try {
                        methods[i].invoke(dd,sqlParams);
                        return true;
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println("没有找到"+interfaceName+"的接口");
        }catch (Exception e){
            return false;
        }
        return false;
    }
}
